<template>
  <div class="musicList">
    <div class="musicTop">
      <div class="title">推荐歌单</div>
      <div class="more">查看更多</div>
    </div>
    <div class="musicContent">
      <van-swipe :loop="false" :show-indicators="false" :width="musicWidth">

                  <!-- <router-link :to="{
                    name:'MusicList',
                        query:{id:music.id}
                    }"> -->


          <van-swipe-item
            v-for="music in state.musicList"
            :key="music.id"
            class="musicItem"
          >

                            <router-link :to="{
                    path:'/itemMusic',
                        query:{id:music.id}
                    }">

            <img :src="music.picUrl" alt="" />
            <span class="playCount">
              <svg class="icon" aria-hidden="true">
                <use xlink:href="#icon-bofang1"></use>
              </svg>
              {{ changeCount(music.playCount) }}
            </span>
            <span class="name">{{ music.name }}</span>

            </router-link>
            
          </van-swipe-item>
                  


      </van-swipe>
    </div>
  </div>
</template>

<script>
import { getMusicList } from "@/request/api/home.js";
import { reactive } from "@vue/reactivity";
import { onMounted } from "vue";
export default {
  //   data() {
  //     return {
  //       musicList: [],
  //     };
  //   },
  //   methods: {
  //     async getMusicList() {
  //       let res = await getMusicList();
  //       this.musicList = res.data.result;
  //       console.log(this.musicList);
  //     },
  //     changeCount(count) {
  //       if (count >= 100000000) {
  //         return (count / 100000000).toFixed(1) + "亿";
  //       } else if (count >= 10000) {
  //         return (count / 10000).toFixed(1) + "万";
  //       }
  //     },
  //   },
  //   mounted() {
  //     this.getMusicList();
  //   },

  //vue3

  setup() {
    const state = reactive({
      musicList: [],
    });
    function getHtmlFontSize() {
      let htmlFontSize = getComputedStyle(window.document.documentElement)[
        "font-size"
      ];

      //以上方法返回的font-size会带单位px，如果不想要px可以做一下处理
      return +htmlFontSize.slice(0, htmlFontSize.indexOf("px"));
    }
    const musicWidth = getHtmlFontSize() * 2;

    function changeCount(count) {
      if (count >= 100000000) {
        return (count / 100000000).toFixed(1) + "亿";
      } else if (count >= 10000) {
        return (count / 10000).toFixed(0) + "万";
      }
    }

    onMounted(async () => {
      let res = await getMusicList();
      // console.log(res);
      state.musicList = res.data.result;
    });

    return {
      changeCount,
      state,
      musicWidth,
    };
  },
};
</script>

<style lang="less" scoped>
.musicList {
  widows: 100%;
  height: 4rem;
  padding: 0.2rem;
  .musicTop {
    width: 100%;
    height: 0.6rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.2rem;
    .title {
      font-size: 0.4rem;
      font-weight: 500;
    }
    .more {
      border: 1px solid #ccc;
      text-align: center;
      line-height: 0.6rem;
      padding: 0 0.2rem;
      border-radius: 0.4rem;
    }
  }
  .musicContent {
    width: 100%;
    height: 3rem;

    .van-swipe {
      height: 100%;

      .musicItem {
        position: relative;
       
        height: 2rem;
        width: 2rem;
        .playCount {
          position: absolute;
          top: 0.05rem;
          right: 0.2rem;
          font-size: 0.2rem;
          color: #fff;
          display: flex;
          justify-content: center;
          align-items: center;
          padding: 0 0.1rem 0 0.1rem;
          background-color: rgba(0, 0, 0, 0.4);
          border-radius: 0.1rem;

          .icon {
            color: #fff;
            width: 0.2rem;
            height: 0.2rem;
          }
        }

        .name {
          display: -webkit-box;
          font-size: 0.22rem;
          width: 100%;
          display: -webkit-box;
          -webkit-box-orient: vertical;
          //   text-overflow: -o-ellipsis-lastline;
          text-overflow: ellipsis;
          overflow: hidden;
        //   white-space: pre;
          line-clamp: 2;
          -webkit-line-clamp: 2;
          color: #333;
        }
        img {
          width: 1.9rem;
          height: 1.9rem;
          border-radius: 0.2rem;
        }
      }
    }
  }
}
</style>